package com.migu.spms.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.migu.spms.common.StringUtil;
import com.migu.spms.constant.Constant;
import com.migu.spms.utils.annotation.type.Column;
import com.migu.spms.utils.exception.ParameterValidateException;
import com.migu.spms.webapi.data.CreateBaselineRequest;
import com.migu.spms.webapi.data.CreateRequireRequest;

/**
 * 
* 项目名称:  SPMS需求管理
* 包:        com.migu.spms.utils 
* 类名称:    RequestValidateUtil.java
* 类描述:    请求参数合法性检查类    
* 创建人:    fw  
* 创建时间:  2017年4月7日 下午2:21:21
 */
public class RequestValidateUtil {
    public static boolean validateCreateRequireRequest(CreateRequireRequest createRequireRequest) throws Exception {
        Field[] fields = CreateRequireRequest.class.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Column column = field.getAnnotation(Column.class);
            if (column == null) {
                continue;
            }
            if (Constant.LogicVariable.NO.equals(column.isNull())) {
                if (field.getType() == String.class && StringUtil.isEmpty((String) (field.get(createRequireRequest)))) {
                    throw new ParameterValidateException(column.name() + "不能为空！");
                } else if (null == field.get(createRequireRequest)) {
                    throw new ParameterValidateException(column.name() + "不能为空！");
                }
            }
        }
        return true;
    }

    public static boolean validateCreateBaselineRequest(CreateBaselineRequest createBaselineRequest) throws Exception {
        Field[] fields = CreateBaselineRequest.class.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Column column = field.getAnnotation(Column.class);
            if (column == null) {
                continue;
            }
            if (Constant.LogicVariable.NO.equals(column.isNull())) {
                if (field.getType() == String.class
                        && StringUtil.isEmpty((String) (field.get(createBaselineRequest)))) {
                    throw new ParameterValidateException(column.name() + "不能为空！");
                } else if (null == field.get(createBaselineRequest)) {
                    throw new ParameterValidateException(column.name() + "不能为空！");
                } else if (field.getType().isAssignableFrom(List.class)
                        && ((List<?>) (field.get(createBaselineRequest))).size() < 1) {
                    throw new ParameterValidateException(column.name() + "不能为空！");
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        CreateBaselineRequest createBaselineRequest = new CreateBaselineRequest();
        createBaselineRequest.setVersion("ss");
        List<Long> list = new ArrayList<>();
        //createBaselineRequest.setFunctionRequireIds(list);
        try {
            new RequestValidateUtil().validateCreateBaselineRequest(createBaselineRequest);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
